package controller;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.TransferHandler;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.Exercise;

import view.NewWorkoutPlan1;
import view.templates.MyJButton;
import view.templates.MyJLabel;

public class NewWorkoutPlanListener1 extends MouseAdapter implements
		ActionListener, ListSelectionListener, DropTargetListener {
	private Controller controller = null;
	private NewWorkoutPlan1 newWorkoutPlan;

	public NewWorkoutPlanListener1(Controller controller) {
		this.controller = controller;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void mousePressed(MouseEvent e) {
		this.newWorkoutPlan = controller.getFrame().getNewWorkoutPlan1();

		if (e.getSource().getClass() == MyJLabel.class) {
			MyJLabel l = (MyJLabel) e.getSource();

			TransferHandler handler = l.getTransferHandler();
			handler.exportAsDrag(l, e, TransferHandler.COPY);
		} else if (e.getSource().getClass() == JList.class) {
			JList<Exercise> list = (JList<Exercise>) e.getSource();

			TransferHandler handler = list.getTransferHandler();
			handler.exportAsDrag(list, e, TransferHandler.COPY);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		this.newWorkoutPlan = controller.getFrame().getNewWorkoutPlan1();

		if (e.getSource().getClass() == MyJButton.class) {
			MyJButton b = (MyJButton) e.getSource();

			if (b == newWorkoutPlan.getNextButton()) {
				controller.getFrame().initNewWorkoutScreen2();
			} else if (b == newWorkoutPlan.getReturnButton()) {
				System.err.println("Back");
				controller.getFrame().initNewWorkoutScreen0();
			}
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
	}

	@Override
	public void dragEnter(DropTargetDragEvent dtde) {
	}

	@Override
	public void dragExit(DropTargetEvent dte) {
	}

	@Override
	public void dragOver(DropTargetDragEvent dtde) {
	}

	@Override
	public void drop(DropTargetDropEvent dtde) {
		this.newWorkoutPlan = controller.getFrame().getNewWorkoutPlan1();
		DataFlavor df = null;
		try {
			df = new DataFlavor(
					"application/x-java-jvm-local-objectref; class=java.lang.String");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		String srcString = "";
		try {
			srcString = (String) dtde.getTransferable().getTransferData(df);
		} catch (UnsupportedFlavorException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		if (dtde.getDropTargetContext().getComponent().getClass() == MyJLabel.class) {
			for (int i = 0; i < 12; i++) {
				if (dtde.getDropTargetContext().getComponent() == newWorkoutPlan
						.getExercises()[i]) {

					MyJLabel source = newWorkoutPlan
							.getLabelFromString(srcString);
					MyJLabel target = newWorkoutPlan.getExercises()[i];
					String trgtString = target.getText();
					if (source != null) {
						target.setText(srcString);
						source.setText(trgtString);
						int pos1 = i;
						int pos2 = newWorkoutPlan
								.getPositionFromString(srcString);
						controller.getCurrentPlan().switchExercises(pos1, pos2);
					} else {
						Exercise selected = newWorkoutPlan.getExerciseList()
								.getSelectedValue();
						target.setText(selected.getName());
						newWorkoutPlan.getExerciseListModel().removeElement(
								selected);
						controller.getCurrentPlan().addExercise(selected, i);
					}
				}
			}
		} else if (dtde.getDropTargetContext().getComponent().getClass() == JList.class) {
			DefaultListModel<Exercise> listModel = newWorkoutPlan
					.getExerciseListModel();
			int pos = newWorkoutPlan.getPositionFromString(srcString);
			if (!srcString.equals(controller.getCurrentPlan().getDummy())) {
				System.err.println("Dummy");
				listModel.addElement(controller.getCurrentPlan().getExerciseAt(
						pos));
				controller.getCurrentPlan().addDummyAt(pos);
				newWorkoutPlan.getExercises()[pos].setText(controller
						.getCurrentPlan().getExerciseAt(pos).getName());
				newWorkoutPlan.sort();
			}
		}
	}

	@Override
	public void dropActionChanged(DropTargetDragEvent dtde) {
	}
}
